package com.bw.myapply_module.myapply

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.kf.common.mvi.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ApplyViewModel : BaseViewModel<ApplyModel, ApplyState>() {
    override fun createState(): ApplyState {
        return ApplyState()
    }

    override fun createModel(): ApplyModel {
        return ApplyModel()
    }

    fun requestApplyAll() {
        var map = HashMap<String, Any>()
        map.put("time", System.currentTimeMillis())
        map.put("userId", SPUtils.getInstance().getInt("userId", -1))
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestApplyAll(map)
                .catch { t: Throwable ->
                    LogUtils.e("HXW", t.message)
                }
                .flowOn(Dispatchers.IO)
                .collect {
                    LogUtils.e("HXW", it.statuesCode)
                    if (it.statuesCode == 200) {
                        mutableContainer.updateUiState {
                            copy(entity = it)
                        }
                    }
                }
        }
    }

}